<?php
/*
Plugin Name: 缩略图重新生成
Plugin URI: https://www.beizigen.com/post/wordpress-thumbnail-regeneration-plug-in-generate-thumbnail/
Description: 换主题后，缩略图尺寸有所改变，此时这个插件可以帮助我们重新生成缩略图。
Version: 1.0.2
Author: 背字根
Author URI: https://www.beizigen.com/
*/

require_once('functions.php');

register_deactivation_hook(__FILE__, 'bzg_generate_thumbnail_delete_cron');
register_uninstall_hook(__FILE__, 'bzg_generate_thumbnail_delete_cron');

function bzg_generate_thumbnail_control()
{
	$message = '';
	if (wp_next_scheduled('generate_thumbnail')) {
		$message = '任务正在执行，请等待任务结束或重启插件以强制终止任务！';
	}

	if (isset($_POST['action']) && $_POST['action'] == '开始生成') {
		update_option('generate_thumbnail_medium_large', $_POST['generate_thumbnail_medium_large']);
		$message = '任务已开始执行，执行完成会邮件通知您！';
		$start = bzg_generate_thumbnail_add_cron();
		if (!$start) {
			$message = '任务正在执行，请等待任务结束或重启插件以强制终止任务！';
		}
	} elseif (isset($_POST['action']) && $_POST['action'] == '查询文章') {
		$args = array(
			'posts_per_page' => -1,
			'post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'trash'),
		);
		$postlist = get_posts($args);
		foreach ($postlist as $post) {
			$content = $post->post_content;
			preg_match_all('/(http|https):\/\/[^\'"]+(\.jpg|\.gif|\.png|\.webp)/', $content, $matches);
			$urls = $matches[0];
			foreach ($urls as $url) {
				$attachment_id = bzg_get_attachment_id_from_src($url);
				if ($attachment_id) {
					$imgsrc = wp_get_attachment_url($attachment_id);
					if ($imgsrc != $url) {
						$postids[] = $post->ID;
					}
				}
			}
		}

		$out_check = '没有包含缩略图的文章';
		if ($postids) {
			$postids = array_unique($postids);
			$out_check = '';
			foreach ($postids as $postid) {
				$post_title = get_the_title($postid);
				$post_link = get_edit_post_link($postid);
				$out_check .= '<a href="' . $post_link . '" target="_blank">' . $post_title . '</a><br />';
			}
		}
	}

	if ($message) {
		echo '<div id="message" class="updated fade" style="padding: 1em;">' . $message . '</div>';
	}

?>
	<div class="wrap">
		<h2>WordPress缩略图重新生成</h2>
		<p>在生成之前请先确保<a href="<?php echo admin_url('options-media.php'); ?>">多媒体设置</a>中已设置好了新的尺寸，并已备份好数据，以防发生意外。</p>
		<form method="post" action="">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row">
							<label for="email">邮箱</label>
						</th>
						<td>
							<input id="email" class="regular-text code" type="email" value="<?php bloginfo('admin_email'); ?>" name="email" disabled="disabled">
							<p class="description">任务执行完成后将处理结果发送至您的邮箱</p>
						</td>
					</tr>

					<tr>
						<th scope="row">
							<label for="medium_large">生成medium_large版本</label>
						</th>
						<td>
							<select id="medium_large" name="generate_thumbnail_medium_large">
								<option value="yes">是</option>
								<option value="no" <?php echo get_option('generate_thumbnail_medium_large') == 'no' ? ' selected="selected"' : ''; ?>>否</option>
							</select>
							<p class="description">medium_large尺寸版本宽为768像素，高不限制，是WordPress为移动设备准备的缩略图版本</p>
						</td>
					</tr>
				</tbody>
			</table>
			<p><input class="button-primary" type="submit" name="action" value="开始生成"></p>
			<p class="description">在点击开始生成按钮后，为了确保任务顺利执行，请点击<a href="<?php bloginfo('url'); ?>/wp-cron.php" target="_blank">访问WordPress定时任务加载页面</a></p>
			<p class="description">要查询哪些文章包含了缩略图或外链的图片，而不是本地原图，请点击：</p>
			<p><input type="submit" class="button" name="action" value="查询文章"></p>

		</form>
		<?php
		if ($out_check) {
			echo '<p>' . $out_check . '</p>';
		}
		?>
	</div>

<?php
}
//添加菜单
function bzg_generate_thumbnail_menu()
{
	if (function_exists('add_options_page')) {
		add_options_page('WordPress缩略图重新生成', '生成缩略图', 'administrator', 'generate-thumbnail', 'bzg_generate_thumbnail_control');
	}
}
add_action('admin_menu', 'bzg_generate_thumbnail_menu');

function bzg_generate_thumbnail_settings_link($action_links, $plugin_file)
{
	if ($plugin_file == plugin_basename(__FILE__)) {
		$settings_link = '<a href="options-general.php?page=generate-thumbnail">工具</a>';
		array_unshift($action_links, $settings_link);
	}
	return $action_links;
}
add_filter('plugin_action_links', 'bzg_generate_thumbnail_settings_link', 10, 2);
?>